#include "StdAfx.h"

#include "OpenCV/cv.h"
#include "OpenCV/cxcore.h"
#include "OpenCV/cxtypes.h"
#include "OpenCV/highgui.h"

#include "Color.h"
#include "Image.h"
#include "DataImage.h"

#include "CheckersImage.h"


CheckersImage::CheckersImage(double sizeOfBlock, const Color& firstColor, const Color& secondColor)
{
	_data = boost::shared_ptr<DataImage> (new DataImage(500, 500));
	
	const double stepSize = sizeOfBlock < 1 ? 500 * sizeOfBlock : 1;
	double stepIter1, stepIter2;
	bool isUsingFirstColor = true, isUsingFirstColorMemo = true;;

	unsigned pixelCounter = 0;
	int y, x;
	for (y = 0, stepIter1 = 0, isUsingFirstColorMemo = true; y < 500; y++, stepIter1++)
	{
		pixelCounter = y * _data->WidthStep();
		if (stepIter1 >= stepSize)
		{
			stepIter1 -= stepSize;
			isUsingFirstColorMemo = !isUsingFirstColorMemo;
		}
		for (x = 0, stepIter2 = 0, isUsingFirstColor = isUsingFirstColorMemo; x < 500; x++, stepIter2++, pixelCounter += 3)
		{
			if (stepIter2 >= stepSize)
			{
				stepIter2 -= stepSize;
				isUsingFirstColor = !isUsingFirstColor;
			}
			_data->SetColor(pixelCounter, isUsingFirstColor ? firstColor : secondColor);
		}
	}
}

Color CheckersImage::GetColor(double x, double y)
{
	return _data->GetColor(x, y);
}

//Does nothing, can't be changed
void CheckersImage::SetColor(unsigned, const Color&)
{}

unsigned CheckersImage::Width()
{
	return _data->Width();
}

unsigned CheckersImage::Height()
{
	return _data->Height();
}